import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/home_entity.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';

class PopularView extends StatelessWidget {
  final List<HomeDataPopularlist> productList;

  PopularView(this.productList);

  @override
  Widget build(BuildContext context) {
    return productList == null || productList.isEmpty
        ? Container()
        : Column(
            children: <Widget>[
              Container(
                height: ScreenUtil.getInstance().setHeight(40),
                alignment: Alignment.center,
                color: Colors.grey[100],
                child: Text(Strings.POPULAR),
              ),
              Container(
                padding: EdgeInsets.all(5.0),
                child: GridView.builder(
                    shrinkWrap: true,
                    itemCount: productList.length,
                    physics: NeverScrollableScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2, childAspectRatio: 0.90),
                    itemBuilder: (BuildContext context, int index) {
                      return _getGridViewItem(context, productList[index]);
                    }),
              )
            ],
          );
  }

  _goGoodsDetail(BuildContext context, HomeDataPopularlist goods) {
    NavigatorUtils.goGoodsDetailPage(context, goods.id);
  }

  Widget _getGridViewItem(
      BuildContext context, HomeDataPopularlist productEntity) {
    return Container(
      child: InkWell(
        onTap: () => _goGoodsDetail(context, productEntity),
        child: Card(
          elevation: 2.0,
          margin: EdgeInsets.all(6.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Expanded(
                  child: CachedImageView(double.infinity,
                      ScreenUtil.instance.setHeight(200.0), productEntity.url)),
              Padding(
                padding: EdgeInsets.only(top: 4.0),
              ),
              Container(
                padding: EdgeInsets.only(left: 4.0, top: 4.0),
                alignment: Alignment.center,
                child: Text(
                  productEntity.name,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: Colors.black54, fontSize: 14.0),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 4.0),
              ),
              Container(
                padding: EdgeInsets.only(left: 4.0, top: 4.0),
                alignment: Alignment.center,
                child: Text(
                  "精品",
                  style: TextStyle(color: Colors.red, fontSize: 12.0),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
